<?php
class ctrl_order extends ctrl_base
{
    public static function index() {
    	//获取open_id 走用户体系取 user_id
    	if(empty($_SESSION['wx_userinfo']['openid'])) {
    		setcookie('backurl',HOST_URL.'/?'.$_SERVER['QUERY_STRING'],0);
    		ctrl_weixin::oauth();
    	}else{
    		setcookie('backurl','');
    		unset($_SESSION['backurl']);
    	}
    	$openid = $_SESSION['wx_userinfo']['openid'];
    	$data = model_user::get_user_by_openid($openid,WEIXINHAO);
    	if(isset($data['user_id']) && $data['user_id'] != '0') {
    		self::smarty() -> assign('user_id',$data['user_id']);
    	} else {
    		$reg_url = HOST_URL.'/index.php?act=register';
    		$msg = "  您好！您还未进行用户注册，不能享受此服务,请点击<a href='".$reg_url."'>这里</a>注册。";
    		model_send::send_text($openid,$msg);
    	}
    	self::smarty() -> display('order.html');
    }
    
    public static function orderSave() {
    	//获取open_id 走用户体系取 user_id
    	if(empty($_SESSION['wx_userinfo']['openid'])) {
    		setcookie('backurl',HOST_URL.'/?'.$_SERVER['QUERY_STRING'],0);
    		ctrl_weixin::oauth();
    	}else{
    		setcookie('backurl','');
    		unset($_SESSION['backurl']);
    	}
    	$data = array();
    	$data['User_ID'] = $_SESSION['wx_userinfo']['openid'];
    	$data['Product_Type'] = trim($_POST['Product_Type']);
    	$data['Product_Spec'] = trim($_POST['Product_Spec']);
    	$data['Product_Grade'] = trim($_POST['Product_Grade']);
    	$data['Buy_Qty'] = trim($_POST['Buy_Qty']);
    	$data['Buy_Type'] = $_POST['Buy_Type'];
    	$data['Buy_Price_Type'] = trim($_POST['Buy_Price_Type']);
    	$data['Buy_Full_Payment'] = trim($_POST['Buy_Full_Payment']);
    	$data['Buy_Settle_Type'] = trim($_POST['Buy_Settle_Type']);
    	$data['Delivery_Type'] = trim($_POST['Delivery_Type']);
    	
    	$data['Order_Arrive_Time'] = trim($_POST['Order_Arrive_Time']);
    	$data['Order_Addr'] = trim($_POST['Order_Addr']);
    	$data['Order_Standard'] = trim($_POST['Order_Standard']);
    	$data['Demand_Quality'] = trim($_POST['Demand_Quality']);    	
    	$data['Demand_Policy'] = trim($_POST['Demand_Policy']);
    	$data['Demand_Service'] = trim($_POST['Demand_Service']);
    	
   	
    	$url = SAVE_ORDER_URL;
    	$postData['Data'] = json_encode($data);
    	$res = model_weixin::http_post($url,$postData);//发送接收采购计划提单接口处理
    	framework_static_function::write_log('用户提交的采购计划数据:'.json_encode($postData),'weixin_order');
    	framework_static_function::write_log('收单接口返回:'.$res,'weixin_order');
    	if($res != false) {
    		$result = json_decode($res,true);
    		if($result['2_Status'] == 1) { //处理成功
    			self::smarty()->assign("result_class",'icon67_status');
				self::smarty()->assign("errMsg",'恭喜您，提交成功！');
				self::smarty()->assign("comment",'我们将在1个工作日内回复您');
				self::smarty()->display('show_result.html');
				exit;
			} else {
    			self::smarty()->assign("result_class",'icon67_status warn');
    			self::smarty()->assign("errMsg",$result['3_Msg']);
    			self::smarty()->display('show_result.html');
    			exit;
    		}
    	} else {
    		self::smarty()->display('error/wx_error.html'); exit;
    	}
    }
}
